package com.fowo.api.documentCirculation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.PageAndStatistics;
import com.fowo.api.common.model.PageSearch;
import com.fowo.api.common.model.R;
import com.fowo.api.documentCirculation.model.DataTaskRuntimeModel;
import com.fowo.api.documentCirculation.model.FromDataPageSearch;
import com.fowo.api.documentCirculation.model.PushDownRequestPo;
import com.fowo.api.documentCirculation.model.PullCancelRequestPo;
import com.fowo.api.documentCirculation.service.DocumentCirculationRuntimeService;
import com.fowo.api.sys.component.AccessManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 单据流转控制器
 * @author yl_ls
 */
@Tag(name = "单据流转控制器")
@RestController
@RequestMapping("/api/document-circulation")
public class DocumentCirculationController {

    @Resource
    private DocumentCirculationRuntimeService runtimeService;

    @Resource
    private AccessManager accessManager;

    /**
     * 下推
     * @param requestPo 下推请求
     */
    @PostMapping("push-down")
    @Operation(summary = "下推")
    public R<?> pushDown(@RequestBody PushDownRequestPo requestPo) {
        return R.run(() -> runtimeService.pushDown(requestPo));
    }

    /**
     * 勾稽
     * @param requestPo 勾稽请求
     */
    @PostMapping("pull")
    @Operation(summary = "勾稽")
    public R<?> pullUp(@RequestBody PushDownRequestPo requestPo) {
        if(!accessManager.canAccess(String.format("dataTaskPull_%d", requestPo.getDataTaskId()))) {
            return R.fail("无权进行此操作");
        }
        return R.run(() -> runtimeService.pullUp(requestPo));
    }

    /**
     * 整单下推
     * @param dataTaskId 事务编号
     * @param idListPo 编号列表包装
     */
    @PostMapping("full-push-down/{dataTaskId}")
    @Operation(summary = "整单下推")
    public R<?> fullPushDown(@PathVariable("dataTaskId") Long dataTaskId, @RequestBody IdListPo idListPo) {
        if(!accessManager.canAccess(String.format("dataTask_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.run(() -> runtimeService.batchPushDownWholeOrder(dataTaskId, idListPo.getIds()));
    }

    @PostMapping("full-pull-cancel")
    @Operation(summary = "勾稽取消")
    public R<?> fullPullCancel(@RequestBody PullCancelRequestPo requestPo){
        if(!accessManager.canAccess(String.format("dataTask_%d", requestPo.getDataTaskId()))) {
            return R.fail("无权进行此操作");
        }
        return R.run(()->runtimeService.pullCancel(requestPo));
    }

    /**
     * 导入勾稽数据与原表做关联
     *
     * @param dataTaskId 任务Id
     * @param toMainId   目标主表Id
     * @return
     */
    @GetMapping("pull_association/{dataTaskId}/{toMainId}")
    @Operation(summary = "导入勾稽数据与原表做关联")
    public R<?> pullAssociation(@PathVariable("dataTaskId") Long dataTaskId, @PathVariable("toMainId") Long toMainId) {
        if (!accessManager.canAccess(String.format("dataTask_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.run(() -> runtimeService.pullAssociation(dataTaskId, toMainId));
    }



    /**
     * 查询是否生成目标数据
     * @param dataTaskId  任务id
     * @param fromMainId  来源主表Id
     * @return
     */
    @GetMapping("get_pull_data/{dataTaskId}/{fromMainId}")
    @Operation(summary = "查询是否生成目标数据")
    public R<?> getPullData(@PathVariable("dataTaskId") Long dataTaskId, @PathVariable("fromMainId") Long fromMainId){
        if(!accessManager.canAccess(String.format("dataTask_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(()->runtimeService.getPullData(dataTaskId, fromMainId));
    }

    /**
     * 删除单据前需判断是否返回数量到原表
     * @param dataTaskId   任务Id
     * @param toMainId      目标主表Id
     * @return
     */
    @GetMapping("getExistToNum/{dataTaskId}/{toMainId}")
    @Operation(summary = "查询删除单据前判断是否数量返回到原表中")
    public R<?> getExistToNum(@PathVariable("dataTaskId") Long dataTaskId,@PathVariable("toMainId") Long toMainId) {
        if (!accessManager.canAccess(String.format("dataTask_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(() -> runtimeService.getExistToNum(dataTaskId, toMainId));
    }

    /**
     * 自动勾稽创建历史日志更新原表记录
     * @param dataTaskId   任务Id
     * @param toMainId      目标主表Id
     * @return
     */
    @GetMapping("pullUp_dataTaskLog/{dataTaskId}/{toMainId}")
    @Operation(summary = "自动勾稽创建历史日志更新原表记录")
    public R<?> pullUpDataTaskLog(@PathVariable("dataTaskId") Long dataTaskId,@PathVariable("toMainId") Long toMainId) {
        if (!accessManager.canAccess(String.format("dataTask_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.run(() -> runtimeService.pullUpDataTaskLog(dataTaskId, toMainId));
    }


    /**
     * 获取一个事务的完整运行时配置
     * @param dataTaskId 事务编号
     */
    @GetMapping("runtime/{dataTaskId}")
    @Operation(summary = "获取一个事务的完整运行时配置")
    public R<DataTaskRuntimeModel> getById(@PathVariable("dataTaskId") Long dataTaskId) {
        if(!accessManager.canAccess(String.format("dataTask_%d,dataTaskPull_%d", dataTaskId, dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(() -> runtimeService.getModelById(dataTaskId));
    }

    /**
     * 获取一个事务的源表分页记录
     */
    @GetMapping("runtime/{dataTaskId}/from-data/{mainDataId}/page")
    @Operation(summary = "获取一个事务的源表分页记录")
    public R<Page<?>> fromDataSearchPage(@PathVariable("dataTaskId")  Long dataTaskId, @PathVariable("mainDataId")  Long mainDataId, FromDataPageSearch pageSearch) {
        if(!accessManager.canAccess(String.format("dataTask_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(() -> runtimeService.fromDataSearchPage(dataTaskId, mainDataId, pageSearch));
    }

    /**
     * 获取一个事务的与目标相关的表分页记录
     */
    @GetMapping("runtime/{dataTaskId}/to-from-data/{toMainDataId}/page")
    @Operation(summary = "获取一个事务的与目标相关的表分页记录")
    public R<PageAndStatistics<?>> toFromDataSearchPage(@PathVariable("dataTaskId")  Long dataTaskId, @PathVariable("toMainDataId")  Long toMainDataId, PageSearch pageSearch) {
        if(!accessManager.canAccess(String.format("dataTaskPull_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(() -> runtimeService.toFromDataSearchPage(dataTaskId, toMainDataId, pageSearch));
    }

    /**
     * 获取一个事务的与目标相关的表分页记录
     */
    @GetMapping("runtime/{dataTaskId}/to-from-main/{toMainDataId}/page")
    @Operation(summary = "获取一个事务的与目标相关的表分页记录")
    public R<Page<?>> toFromMainSearchPage(@PathVariable("dataTaskId")  Long dataTaskId, @PathVariable("toMainDataId")  Long toMainDataId, PageSearch pageSearch) {
        if(!accessManager.canAccess(String.format("dataTaskPull_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(() -> runtimeService.toFromMainSearchPage(dataTaskId, toMainDataId, pageSearch));
    }

    @GetMapping("runtime/{dataTaskId}/to-data/{toMainDataId}")
    @Operation(summary = "通过历史记录返回勾稽缓存区的最后状态")
    public R<?> toDataSearchPage(@PathVariable("dataTaskId") Long dataTaskId, @PathVariable("toMainDataId")  Long toMainDataId) {
        if(!accessManager.canAccess(String.format("dataTaskPull_%d", dataTaskId))) {
            return R.fail("无权进行此操作");
        }
        return R.data(() -> runtimeService.toDataList(dataTaskId, toMainDataId));
    }
}
